package we3d.renderer 
{
	import we3d.View3d;
	import we3d.layer.Layer;
	import we3d.mesh.Face;
	import we3d.math.Matrix3d;
	
	public class DistanceSort implements IPolyRenderer 
	{
		public function DistanceSort () {}
		
		public function drawPoly (view:View3d, p:Face) :void {
			var lyr:Layer = view.currentLayer;
			var gv:Matrix3d = view.scene.cam.transform.gv;
			var ogv:Matrix3d = p.so.transform.gv;
			
			var ax:Number = ogv.a*p.ax + ogv.e*p.ay + ogv.i*p.az + ogv.m;
			var ay:Number = ogv.b*p.ax + ogv.f*p.ay + ogv.j*p.az + ogv.n;
			var az:Number = ogv.c*p.ax + ogv.g*p.ay + ogv.k*p.az + ogv.o;
			
			var x:Number = gv.m - ax;
			var y:Number = gv.n - ay;
			var z:Number = gv.o - az;
			
			p.z = -(Math.sqrt(x*x + y*y + z*z)+p.sortFar);
			
			lyr.polys[lyr.pCount++] = p;
		}
		
	}
	
}